package com.zhouyu.exampledesignmoudle.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * JDK代理操作类
 *
 */
public class ProxyHandler implements InvocationHandler {
    //被代理对象
    private Object target;

    /**
    * @description :创建代理对象
    * @param object :目标对象
    * @return {@link null} 代理对象
    * @author zhouyu
    * @date 2020/5/8 22:05
    */
    public Object bind(Object object) {
        this.target = object;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        doBefore();
       //执行代码
        Object result = method.invoke(target, args);
        doAfter();
        return result;
    }

    private void doAfter() {
        System.out.println("jdk after method invoke...");
    }
    private void doBefore() {
        System.out.println("jdk before method invoke...");
    }
}
